package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.dao.UserInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.UserInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.UserInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.model.UserSessionInfo;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.RegisterUserNameCheckRequest;
import cc.rengu.igas.bsps.facade.response.RegisterUserNameCheckResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @version 1.0
 * @author: zhangxuran
 * @Date: 2020/4/2 11:37
 * @Description: 注册用户名校验
 */
public class RegisterUserNameCheckService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        RegisterUserNameCheckRequest request = new RegisterUserNameCheckRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        RegisterUserNameCheckRequest registerUserNameCheckRequest = (RegisterUserNameCheckRequest) request;
        String instId = registerUserNameCheckRequest.getHeader().getInstId();
        String userLoginId = registerUserNameCheckRequest.getUserLoginId();
        String sessionId = registerUserNameCheckRequest.getHeader().getSession();
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userLoginId)) {
            rglog.error("必填性校验失败！instId<{}>,userLoginId:<{}>", instId, userLoginId);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        BizResponse<RegisterUserNameCheckResponse> bizResponse = new BizResponse<>();
        RegisterUserNameCheckResponse registerUserNameCheckResponse = new RegisterUserNameCheckResponse();
        //默认不存在
        String flag = BspsAppParamConstant.FLAG_Y;
        UserInfoMapper userInfoMapper = new UserInfoMapperImpl();
        List<UserInfo> userInfoList = userInfoMapper.selectBspsUserInfoByUserLoginId(instId, userLoginId);
        String userSessionString = RedisUtil.onceGet(sessionId);
        UserSessionInfo userSessionInfo = JSON.parseObject(userSessionString, UserSessionInfo.class);
        if (CollectionUtils.isEmpty(userInfoList)) {
            //用户名已存在
            flag = BspsAppParamConstant.FLAG_N;
        }
        registerUserNameCheckResponse.setVerifyResult(flag);
        bizResponse.setResult(registerUserNameCheckResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        RegisterUserNameCheckResponse registerUserNameCheckResponse = (RegisterUserNameCheckResponse) bizResponse.getResult();
        ConvertUtil.convertInput(registerUserNameCheckResponse);
    }
}
